<?php

namespace App\Http\Controllers\Admins;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admins\RoleRequest;
use App\Repositories\BLL\RoleBLL;
use App\Repositories\BLL\PermissionBLL;
use Illuminate\Support\Facades\Route;

class RoleController extends Controller
{
	private $roleBLL,$permissionBLL;
	private $currentRouteName;
	  	

    function __construct(RoleBLL $role,PermissionBLL $permission)
    {
    		$this->currentRouteName = Route::currentRouteName();
        $this->roleBLL = $role;
        $this->permissionBLL = $permission;
    }
	
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $addButton = sp_get_AddActionButton($this->currentRouteName);
        return view('admin.role.index',
        					["addButton"=>$addButton]);
    }
	
	public function ajaxIndex()
    {
        $result = $this->roleBLL->ajaxIndex($this->currentRouteName);
        return response()->json($result);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.role.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(RoleRequest $request)
    {
        $this->roleBLL->createEntity($request->all());
        return redirect(route('role.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $role = $this->roleBLL->editView($id);
        return view('admin.role.show',
						["role"=>$role]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $role = $this->roleBLL->editView($id);
        return view('admin.role.edit',
						["role"=>$role]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(RoleRequest $request, $id)
    {
        $this->roleBLL->updateEntity($request->all(),$id);
        return redirect(route('role.index'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->roleBLL->destroyEntity($id);
        return redirect(route('role.index'));
    }
	
	public function auth($id){
		$permissions = $this->permissionBLL->getAllPermissionMenu();
		$permissionByRole= $this->roleBLL->getPermissionByRole($id);
//		dd($permissionByRole->all());
		return view('admin.role.auth',
						['id'=>$id
						,'permissions'=>$permissions
						,'permissionbyrole'=>$permissionByRole]);
	}
	public function authpost(Request $request,$id){
		$this->roleBLL->postAuth($request->all(),$id);
		return redirect(route('role.index'));
	}
}
